package com.bnlr.lrparking.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.blankj.utilcode.util.BarUtils;
import com.bnlr.lrparking.R;

/**
 * @author : tian
 *         time   : 2018/03/28
 *         desc   : 条件筛选框
 */

public class ConditionPopup {
    private View view;
    private Context context;
    private PopupWindow popupWindow;
    private int screenWidth;
    private int screenHeight;
    private boolean isMatch = false;
    public ConditionPopup(Context context, View view) {
        this.view = view;
        this.context = context;
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        screenWidth = wm.getDefaultDisplay().getWidth();
        screenHeight = wm.getDefaultDisplay().getHeight();
    }
    public void  setTag () {
        this.isMatch = true;
    }
    public void show () {
        if (view == null) {
            throw new NullPointerException("view not allow null");
        }
        if(popupWindow == null) {
            LinearLayout layout = new LinearLayout(context);
            LinearLayout.LayoutParams params;
            if (isMatch) {
                params = new LinearLayout.LayoutParams(screenWidth, LinearLayout.LayoutParams.MATCH_PARENT);
                view.setLayoutParams(params);
                layout.addView(view);
                layout.setGravity(Gravity.RIGHT);
                //layout.setBackgroundColor(Color.argb(80, 0, 0, 0));
                popupWindow = new PopupWindow(layout,screenWidth,screenHeight- BarUtils.getStatusBarHeight());

            } else {
                params = new LinearLayout.LayoutParams(screenWidth*2/3, LinearLayout.LayoutParams.MATCH_PARENT);
                view.setLayoutParams(params);
                layout.addView(view);
                layout.setGravity(Gravity.RIGHT);
                //layout.setBackgroundColor(Color.argb(80, 0, 0, 0));
                popupWindow = new PopupWindow(layout,screenWidth,screenHeight);
            }
            popupWindow.setFocusable(true);
            popupWindow.setBackgroundDrawable(new BitmapDrawable());
            popupWindow.setOutsideTouchable(true);
            popupWindow.setAnimationStyle(R.style.popup_condition_anim);
            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    setWindowAlpha(1.0f);
                }
            });
            layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    hidePopup();
                }
            });
        }
        if (Build.VERSION.SDK_INT < 24) {
            setWindowAlpha(0.5f);
            popupWindow.showAtLocation(view, Gravity.RIGHT, 0, 0);
        } else {
            setWindowAlpha(0.5f);
            popupWindow.showAtLocation(view, Gravity.RIGHT, 0, 0);
            popupWindow.update();
        }
    }
    public void hide() {
        hidePopup();
    }
    private void hidePopup() {
        if(popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();

        }
    }
    private void setWindowAlpha (float alpha) {
        WindowManager.LayoutParams lp = ((Activity)context).getWindow()
                .getAttributes();
        lp.alpha = alpha;
        ((Activity) context).getWindow().setAttributes(lp);
    }
}
